package org.webscale.json.cmd;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.webscale.ApplicationException;

public class JsonCommandRegistry {
	private static final Logger log = Logger.getLogger(JsonCommandRegistry.class);
    private Map<String, String> commands = new HashMap<String, String>();
	public Map<String, String> getCommands() {
		return commands;
	}
	public void setCommands(Map<String, String> commands) {
		this.commands = commands;
	}
    
	public String getClassNameForCommand(String cmdName){
		return commands.get(cmdName);
	}

	public Class<AppEngineCmd> getClassForCommand(String cmdName){
		String clazzName = commands.get(cmdName);
		Class<AppEngineCmd> clazz = null;
		try {
			clazz = (Class<AppEngineCmd>) Class.forName(clazzName);
		} catch (ClassNotFoundException e) {
			log.error("Cannot load the class ", e);
			throw new ApplicationException(e);
		}
		return clazz;
	}
	
	public AppEngineCmd getAppEngineCmd(String cmdName){
		Class<AppEngineCmd> clazz = getClassForCommand(cmdName);
		AppEngineCmd cmd = null;
		try {
			cmd = clazz.newInstance();
		} catch (InstantiationException e) {
			log.error("InstantiationException ", e);
			throw new ApplicationException(e);
		} catch (IllegalAccessException e) {
			log.error("IllegalAccessException ", e);
			throw new ApplicationException(e);
		}
		return cmd;
	}
	
	
	
	
}
